﻿
using DataService.Common;
using DataService.Core.ServiceDTO;
using DataService.DBSql;
using DataService.Remote.DataAccessLayer.DBSql;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace DataService.Extensions
{
    public static class DBSchemaSqlExtensions
    {
        private static Dictionary<DatabaseType, DBSchemaSql> _dbSql = new Dictionary<DatabaseType, DBSchemaSql>();
        static DBSchemaSqlExtensions()
        {
            Register<SqlserverSql>(DatabaseType.SqlServer);
            Register<SQLiteSql>(DatabaseType.SQLite);
            Register<MySqlSql>(DatabaseType.MySql);
            Register<OracleSql>(DatabaseType.Oracle);
            Register<DMSql>(DatabaseType.DaMeng);
            Register<MongoDBSql>(DatabaseType.MongoDB);
        }
        private static void Register<T>(DatabaseType dbType) where T : DBSchemaSql, new() => _dbSql[dbType] = new T();

        private static DBSchemaSql CreateSqlExtend(DatabaseType dbType)
        {
            var type = _dbSql[dbType]?.GetType();
            var sqlExtend = Activator.CreateInstance(type, true) as DBSchemaSql;
            return sqlExtend;
        }
        public static string GetInsertSql<T>(DatabaseType dbType, string fullTableName)
        {
            var sqlExtend = CreateSqlExtend(dbType);
            string sql = sqlExtend.GetInsertSql<T>(fullTableName);
            return sql;
        }
        public static string GetUpdateSql<T>(DatabaseType dbType, List<string> pkFields, string fullTableName)
        {
            var sqlExtend = CreateSqlExtend(dbType);
            return sqlExtend.GetUpdateSql<T>(pkFields, fullTableName);
        }
        public static string GetUpdateSql<T>(DatabaseType dbType, List<string> pkFields, Expression<Func<T, object>> fieldFunc, string fullTableName)
        {
            var sqlExtend = CreateSqlExtend(dbType);
            return sqlExtend.GetUpdateSql<T>(pkFields, fieldFunc, fullTableName);
        }
        public static string GetDeleteSql<T>(DatabaseType dbType, Expression<Func<T, bool>> whereExpression, ref object paramValue, string fullTableName)
        {
            var sqlExtend = CreateSqlExtend(dbType);
            return sqlExtend.GetDeleteSql<T>(whereExpression, ref paramValue, fullTableName);
        }
        /// <summary>
        /// 构建查询语句
        /// </summary>
        /// <param name="fieldFunc">若为null 则查询 *；否则查询指定字段</param>
        /// <returns></returns>
        public static string GetSelectSql<T>(DatabaseType dbType, Expression<Func<T, bool>> whereExpression, ref object paramValue, string fullTableName, Expression<Func<T, object>> fieldFunc)
        {
            var sqlExtend = CreateSqlExtend(dbType);
            return sqlExtend.GetSelectSql<T>(whereExpression, ref paramValue, fullTableName, fieldFunc);
        }
        public static string GetPageSelectSql<T>(DatabaseType dbType, Expression<Func<T, bool>> whereExpression, ref object paramValue, string fullTableName, Expression<Func<T, object>> fieldFunc)
        {
            var sqlExtend = CreateSqlExtend(dbType);
            return sqlExtend.GetPageSelectSql<T>(whereExpression, ref paramValue, fullTableName, fieldFunc);
        }

        public static string GetScalarCountSql<T>(DatabaseType dbType, Expression<Func<T, bool>> whereExpression, ref object paramValue, string fullTableName, string databaseName)
        {
            var sqlExtend = CreateSqlExtend(dbType);
            return sqlExtend.GetSelectScalarCountSql<T>(whereExpression, ref paramValue, fullTableName, databaseName);
        }

    }
}
